package dhbw.GUI.Panels;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

import dhbw.Data.Language;
import dhbw.Data.Semester;
import dhbw.Data.Settings;
import dhbw.Data.Vorlesung;
import dhbw.Roles.Dozent;
import dhbw.Roles.Studiengangsleiter;
import Main.MyController;

public class PanelNewVorlesung extends JPanel implements ActionListener {

	private MyController myc;
	private JFrame parent;
	private JButton bt_add, bt_cancel;
	private JTextField tf_bezeichnung;
	private JComboBox cb_semester, cb_lecturer;
	private JLabel l_stgang;

	public PanelNewVorlesung(MyController myc, JFrame parent) {
		this.myc = myc;
		this.parent = parent;
		build();
	}

	private void build() {
		this.setBackground(Settings.BG_COLOR);
		bt_add = new JButton(Language.ADD[myc.getLanguage()]);
		bt_add.addActionListener(this);
		bt_cancel = new JButton(Language.CANCEL[myc.getLanguage()]);
		bt_cancel.addActionListener(this);
		tf_bezeichnung = new JTextField();
		cb_semester = new JComboBox();
		cb_lecturer = new JComboBox();
		l_stgang = new JLabel();
		
		CellConstraints cc = new CellConstraints();
		String cols = "16dlu,pref,6dlu,pref,16dlu";
		String rows = "6dlu,pref,6dlu,pref,6dlu,pref,6dlu,pref,6dlu,pref,6dlu";
		FormLayout grid = new FormLayout(cols, rows);
		this.setLayout(grid);

		this.add(new JLabel(Language.MAJOR[myc.getLanguage()]), cc.xy(2, 2));
		this.add(new JLabel(Language.SEMESTER[myc.getLanguage()]), cc.xy(2, 6));
		this.add(new JLabel(Language.COURSE[myc.getLanguage()]), cc.xy(2, 4));

		this.add(l_stgang, cc.xy(4, 2));
		this.add(cb_semester, cc.xy(4, 6));
		this.add(tf_bezeichnung, cc.xy(4, 4));

		this.add(new JLabel(Language.LECTURER[myc.getLanguage()]), cc.xy(2, 8));
		this.add(cb_lecturer, cc.xy(4, 8));

		this.add(bt_add, cc.xy(2, 10));
		this.add(bt_cancel, cc.xy(4, 10));
		l_stgang.setText(((Studiengangsleiter)this.myc.getActiveUser()).getStudyPath().getBezeichnung());
		fillLists();
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource().equals(bt_add)) {
			if (filledRight(this)) {
				Studiengangsleiter stgl = ((Studiengangsleiter)this.myc.getActiveUser());
				
				JOptionPane.showMessageDialog(this.getComponent(0), new String(
						Language.FIELDSOKNEWVORLESUNG[this.myc.getLanguage()]),
						Language.INFO[this.myc.getLanguage()], 1);
				Vorlesung v1 = new Vorlesung();
				v1.setBezeichnung(tf_bezeichnung.getText());
				v1.setLecturer((Dozent)cb_lecturer.getSelectedItem());
				v1.setSemester(Integer.parseInt(cb_semester.getSelectedItem().toString().split(" ")[1]));
				v1.setStudyPath(stgl.getStudyPath());
				stgl.newLecture(v1);
				
				parent.dispose();
			} else {
				JOptionPane.showMessageDialog(this.getComponent(0), new String(
						Language.FIELDSAREEMPTY[this.myc.getLanguage()]),
						Language.ERROR[this.myc.getLanguage()], 0);
			}
		}

		if (e.getSource().equals(bt_cancel)) {
			parent.dispose();
		}
	}

	private boolean filledRight(JPanel panel) {
		boolean rtn = true;
		Component[] components = panel.getComponents();
		Component component = null;
		for (int i = 0; i < components.length; i++) {
			component = components[i];
			if (component instanceof JTextField) {
				String text = ((JTextField) component).getText();
				if (text.equals("") || text.trim().equals("")) {
					rtn = false;
				}
			}
		}
		return rtn;
	}
	private void fillLists()
	{
		ArrayList<Dozent> dozis = ((Studiengangsleiter)this.myc.getActiveUser()).getStudyPath().getLecturerList();
		for(int i=0;i<dozis.size() ; i++)
		{
			cb_lecturer.addItem(dozis.get(i));
		}
		
		for( int i=0;i<6;i++)
		{
			cb_semester.addItem("Semester "+(i+1));
		}
	}
}
